package com.neiquan.backstage.resourec_manage.webaction;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.BackendErrorCode;
import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.common.util.Paging;
import com.neiquan.backstage.common.util.Sessions;
import com.neiquan.backstage.common.util.Strings;
import com.neiquan.backstage.pub.bsc.dao.po.AdminUser;
import com.neiquan.backstage.pub.bsc.dao.po.Partner;
import com.neiquan.backstage.resourec_manage.PartnerService;

@Controller
@RequestMapping(value = "alliancePartner", produces = { "text/json;charset=UTF-8" })
public class PartnerAction {

	private static Class<?> poc = Partner.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private PartnerService alliancePartnerService;

	// 添加Partner
	@ResponseBody
	@RequestMapping(value = { "addPartner" })
	public String addStation(String name, String loge, String description, String linkman, String phone, String mail,
			String address, String projectType, String projectDesc, Integer teamSize,HttpSession session) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		Partner entity = new Partner();
		if (alliancePartnerService.queryByName(Strings.trimAndNotNull(name),memberId) != null) {
			return CommonResp.buildErrorResp(BackendErrorCode.already_exist).toJsonString();
		}
		entity.setName(Strings.trimAndNotNull(name));
		entity.setLoge(Strings.trimAndNotNull(loge));
		entity.setDescription(Strings.trimAndNotNull(description));
		entity.setLinkman(Strings.trimAndNotNull(linkman));
		entity.setPhone(Strings.trimAndNotNull(phone));
		entity.setMail(Strings.trimAndNotNull(mail));
		entity.setAddress(Strings.trimAndNotNull(address));
		entity.setMemberId(memberId);
		alliancePartnerService.add(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	// 分页查询
	@ResponseBody
	@RequestMapping(value = { "list" })
	public String list(int pageIndex, int pageSize,HttpSession session) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<?> paging = extraSpringHibernateTemplate.getHibernateTemplate().find("From Partner where memberId=?", memberId);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	// 通过Id查询Partner
	@ResponseBody
	@RequestMapping(value = { "queryById" })
	public String queryById(int id) {
		Partner entity = alliancePartnerService.queryById(id);
		if (entity == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		return CommonResp.buildSuccessResp(entity).toJsonString();
	}

	// 修改partner
	@ResponseBody
	@RequestMapping(value = { "edit" })
	public String edit(int id, String name, String loge, String description, String linkman, String phone, String mail, String address) {
		Partner entity = alliancePartnerService.queryById(id);
		if (entity == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		entity.setName(Strings.trimAndNotNull(name));
		entity.setLoge(Strings.trimAndNotNull(loge));
		entity.setDescription(Strings.trimAndNotNull(description));
		entity.setLinkman(Strings.trimAndNotNull(linkman));
		entity.setPhone(Strings.trimAndNotNull(phone));
		entity.setMail(Strings.trimAndNotNull(mail));
		entity.setAddress(Strings.trimAndNotNull(address));
		extraSpringHibernateTemplate.getHibernateTemplate().update(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	// 条件查询Partner
	@ResponseBody
	@RequestMapping(value = "search")
	public String search(String linkman, String phone,HttpSession session) throws ParseException {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<Criterion> criterions = new ArrayList<>();
		if (StringUtils.isNoneBlank(linkman)) {
			criterions.add(Restrictions.like("linkman", "%" + linkman + "%"));
		}
		if (StringUtils.isNoneBlank(phone)) {
			criterions.add(Restrictions.like("phone", "%" + phone + "%"));
		}
		criterions.add(Restrictions.eq("memberId",memberId));
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("partnerId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	// 删除Partner
	@ResponseBody
	@RequestMapping(value = { "remove" })
	public String remove(int id) {
		Partner entity = alliancePartnerService.queryById(id);
		if (entity == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		extraSpringHibernateTemplate.getHibernateTemplate().delete(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}
}
